<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Adcategory extends CI_Controller {
    public function __construct(){
        parent::__construct();
        set_validation_error_delimiter();
        $this->load->model('categorymodel');
    }

    function index (){
        $data['subtitle'] ='Danh sách danh mục';
        $data['submenu'] = 'admin/site/category/submenu';
        $data['site'] = 'tp-admin/site/category/category-listview';
        $data['message'] = null;
        $page = $this->uri->segment(4);
        if($page<=1 || $page % PAGE_SIZE != 0)
            $page = 0;
        $category = $this->categorymodel->load($page);
        $data['list'] = $category['list'];
        $total = $category['total'];
        $this->session->set_userdata( array('currentlink' => current_url()));
        if($this->session->userdata('cate_act')!=''){
            $data['message']= message_box('n-success',$this->session->userdata('cate_act'));
            $this->session->unset_userdata('cate_act');
        }
        if($this->session->userdata('cate_err')!=''){
            $data['message']= message_box('n-error',$this->session->userdata('cate_act'));
            $this->session->unset_userdata('cate_err');
        }
        $link = ADMIN_PAGE_CATEGORY;
        set_paging($link,$total,4);
        $this->load->view('tp-admin/template',$data);
    }

    function add(){
        $data['subtitle'] = 'Thêm danh mục mới';
        $data['submenu'] = 'admin/site/category/submenu';
        $data['site'] = 'admin/site/category/editor';
        $data['list'] = $this->categorymodel->load_cate();
        $data['message'] = null;
        if($this->input->post('submit') && $this->check_validation()){
            $info = $this->post_input();
            $info['category_id'] = generate_number_code(8);
            $info['category_name_segment'] = utf8_to_ascii($info['category_name']);
            $info['category_tag'] = replace_space_to_dash($info['category_name'],', ').', ' . replace_space_to_dash($info['category_name_segment'],', ');
            $info['category_name_segment'] = replace_space_to_dash($info['category_name_segment'],'-');
            if($this->categorymodel->add($info)){
                $this->session->set_userdata(array('cate_act' => 'Tạo danh mục mới thành công'));
                redirect($this->session->userdata('currentlink'));
            }
            else{
                $data['message']= message_box('n-error','Tạo danh mục không thành công. Kiểm tra lại nội dung');
            }
        }
        $this->load->view('admin/template',$data);
    }

    function edit(){
        $data['subtitle'] = 'Cập nhật danh mục';
        $data['submenu'] = 'admin/site/category/submenu';
        $data['site'] = 'admin/site/category/editor';
        $data['list'] = $this->categorymodel->load_cate();
        $cate_id = $this->uri->segment(4);
        $data['info'] = array();
        $data['message'] = null;
        foreach($data['list'] as $item){
            if($item->category_id == $cate_id){
                $data['info'] = array(
                    'category_id' => $item->category_id,
                    'category_name' => $item->category_name,
                    'category_order' => $item->category_order,
                    'category_display_state' => $item->category_display_state,
                    'category_parent_id' => $item->category_parent_id
                );
                break;
            }
        }
        if($this->input->post('submit') && $this->check_validation()){
            $info = $this->post_input(); print_r($info);
            $info['category_name_segment'] = utf8_to_ascii($info['category_name']);
            $info['category_tag'] = replace_space_to_dash($info['category_name'],', ').', ' . replace_space_to_dash($info['category_name_segment'],', ');
            $info['category_name_segment'] = replace_space_to_dash($info['category_name_segment'],'-');
            if($this->categorymodel->edit($info,$cate_id)){

                $this->session->set_userdata(array('cate_act' => 'Cập nhật danh mục thành công'));
                redirect($this->session->userdata('currentlink'));
            }
            else{
                $data['message']= message_box('n-error','Cập nhật danh mục không thành công. Kiểm tra lại nội dung');
            }
        }
        $this->load->view('admin/template',$data);
    }

    function remove(){
        $cate_id = $this->uri->segment(4);
        $info = array(
            'category_is_delete' => 1
        );
        if($this->categorymodel->remove($info,$cate_id)){

            $this->session->set_userdata(array('cate_act' => 'Đã xoá danh mục thành công'));
            redirect($this->session->userdata('currentlink'));
        }
        else{
            $this->session->set_userdata(array('cate_err' => 'Xoá danh mục không thành công. Hãy thử lại'));
            redirect($this->session->userdata('currentlink'));
        }
    }

    function post_input(){
        $info = array(
            'category_name' => $this->input->post('name'),
            'category_parent_id' => $this->input->post('group'),
            'category_order' => $this->input->post('order'),
            'category_display_state' => $this->input->post('display')
        );
        return $info;
    }
    function check_validation(){
         $check_validation=array(
             array(
                 'field'=>'name',
                 'label'=>'Tên danh mục',
                 'rules'=>'trim|required'
             ),
             array(
                 'field'=>'order',
                 'label'=>'Thứ tự hiển thị',
                 'rules'=>'trim|required|integer|greater_than[0]'
             )
         );
         $this->form_validation->set_rules($check_validation);
         return  $this->form_validation->run();
    }
}